package com.boarsoft.boar.soagov.svc.biz;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.soagov.entity.SvcInst;
import com.boarsoft.boar.soagov.svc.SvcInstBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("SvcInstBiz")
public class SvcInstBizImpl extends SimpleBizImpl implements SvcInstBiz {

	@Override
	@Transactional(readOnly = true)
	public SvcInst get(String id) {
		return dao.get(SvcInst.class, id);
	}

	@Override
	@Transactional(readOnly = true)
	public PagedResult<SvcInst> list(String svcId, String env, String key, String orderBy, int pageNo, int pageSize) {
		StringBuffer sb = new StringBuffer();
		if (Util.strIsNotEmpty(svcId)) {
			sb.append(" and svcId='").append(svcId).append("'");
		}
		if (Util.strIsNotEmpty(env)) {
			sb.append(" and env='").append(env).append("'");
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and code='").append(key).append("'");
		}
		if (Util.strIsNotEmpty(orderBy)) {
			sb.append(" order by ").append(orderBy);
		}
		int total = dao.getTotal(SvcInst.class, sb.toString());
		List<SvcInst> lt = dao.list(SvcInst.class, sb.toString(), pageNo, pageSize);
		return new PagedResult<SvcInst>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional
	public void delete(String id) {
		dao.delete(SvcInst.class, id);
	}

	@Override
	@Transactional(readOnly = true)
	public SvcInst find(String svcId, String ip, int port) {
		StringBuilder sb = new StringBuilder();
		sb.append("and svcId='").append(svcId).append("' and ip='");
		sb.append(ip).append("' and port=").append(port);
		return dao.find(SvcInst.class, sb.toString());
	}

	@Override
	@Transactional
	public void update(SvcInst o) {
		dao.update(o);
	}

	@Override
	@Transactional
	public void save(SvcInst o) {
		dao.save(o);
	}

}
